<template>
  <el-card>
    <el-form :model="userObj" label-width="80px">
      <!-- <el-form-item label="id">
          <el-input v-model="userObj._id" disabled style="width: 300px;"></el-input>
        </el-form-item> -->
      <el-form-item label="用户名">
        <el-input
          v-model="userObj.username"
          disabled
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="userObj.nickname" style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="头像">
        <el-upload
          class="avatar-uploader"
          action="/api/crm/img"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :disabled="avatarFlag"
        >
          <el-image
            v-if="avatarFlag"
            :src="userObj.avatar"
            class="avatar"
          ></el-image>
          <!-- <el-image v-if="false" :src="userObj.avatar"></el-image> -->
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
        <el-link icon="Edit" style="margin: 0px 10px" @click="changeAvatar">
          修改头像
        </el-link>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitChange">保存</el-button>
        <el-button @click="cancelUpdate">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin-top: 10px">
    <!-- <router-link to="/user/center/qyyjt">测试</router-link>
    <router-view v-slot="{ Component }">
    <component :is="Component" />
  </router-view> -->
    <qyyjt></qyyjt>
  </el-card>
</template>
<script setup lang="ts">
import useUserStore from '@/store/modules/user'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import qyyjt from './qyyjt.vue'

let avatarFlag = ref(true)
let userStore = useUserStore()
let userObj = ref(userStore.userObj)
let $router = useRouter()

const changeAvatar = () => {
  avatarFlag.value = false
}
const handleAvatarSuccess = (resp, upload) => {
  if (resp.code === 200) {
    userObj.value.avatar = resp.data.url
    avatarFlag.value = true
  }
}
const submitChange = () => {
  userStore.userUpdate(userObj.value)
  $router.push('/') //回首页
}
const cancelUpdate = () => {
  $router.push('/')
}
</script>
<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  text-align: center;
}
</style>
